<?php
/*
** Zabbix
** Copyright (C) 2001-2020 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/


/**
 * @var CView $this
 */
?>

<script type="text/x-jquery-tmpl" id="row_expr">
	<?= (new CRow([
			(new CComboBox('expressions[#{rowNum}][expression_type]', null, null, expression_type2str()))
				->onChange('onChangeExpressionType(this, #{rowNum})'),
			(new CTextBox('expressions[#{rowNum}][expression]', '', false, 255))
				->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
				->setAriaRequired(),
			(new CComboBox('expressions[#{rowNum}][exp_delimiter]', null, null, expressionDelimiters()))
				->addStyle('display: none;'),
			new CCheckBox('expressions[#{rowNum}][case_sensitive]'),
			(new CCol(
				(new CButton('expressions[#{rowNum}][remove]', _('Remove')))
					->addClass(ZBX_STYLE_BTN_LINK)
					->addClass('element-table-remove')
			))->addClass(ZBX_STYLE_NOWRAP)
		]))
			->addClass('form_row')
			->setAttribute('data-index', '#{rowNum}')
			->toString()
	?>
</script>

<script type="text/x-jquery-tmpl" id="testTableRow">
	<?= (new CRow([
			'#{type}', '#{expression}', (new CSpan('#{result}'))->addClass('#{resultClass}')
		]))
			->addClass('test_row')
			->toString()
	?>
</script>

<script type="text/x-jquery-tmpl" id="testCombinedTableRow">
	<?= (new CRow([
			(new CCol(_('Combined result')))->setColspan(2), (new CSpan('#{result}'))->addClass('#{resultClass}')
		]))
			->addClass('test_row')
			->toString()
	?>
</script>

<script>
	function onChangeExpressionType(obj, index) {
		if (obj.value === '<?= EXPRESSION_TYPE_ANY_INCLUDED ?>') {
			jQuery('#expressions_' + index + '_exp_delimiter').show();
		}
		else {
			jQuery('#expressions_' + index + '_exp_delimiter').hide();
		}
	}

	(function($) {
		/**
		 * Object to manage expression related GUI elements.
		 * @type {Object}
		 */
		window.zabbixRegExp = {

			/**
			 * Template for expression row of testing results table.
			 * @type {String}
			 */
			testTableRowTpl: new Template($('#testTableRow').html()),

			/**
			 * Template for combined result row in testing results table.
			 * @type {String}
			 */
			testCombinedTableRowTpl: new Template($('#testCombinedTableRow').html()),

			/**
			 * Send all expressions data to server with test string.
			 *
			 * @param {string} string Test string to test expression against
			 *
			 * @return {jqXHR}
			 */
			testExpressions: function(string) {
				var ajaxData = {
					testString: string,
					expressions: {}
				};

				$('#testResultTable').css({opacity: 0.5});

				$('#tbl_expr .form_row').each(function() {
					var index = $(this).data('index');

					ajaxData.expressions[index] = {
						expression : $('#expressions_' + index + '_expression').val(),
						expression_type : $('#expressions_' + index + '_expression_type').val(),
						exp_delimiter : $('#expressions_' + index + '_exp_delimiter').val(),
						case_sensitive : $('#expressions_' + index + '_case_sensitive').is(':checked') ? '1' : '0'
					}
				});

				var url = new Curl('zabbix.php');
				url.setArgument('action', 'regex.test');

				return $.post(
					url.getUrl(),
					{ajaxdata: ajaxData},
					$.proxy(this.showTestResults, this),
					'json'
				);
			},

			/**
			 * Update test results table with data received form server.
			 *
			 * @param {Object} response ajax response
			 */
			showTestResults: function(response) {
				var tplData, hasErrors, obj = this;

				$('#testResultTable .test_row').remove();

				hasErrors = false;

				$('#tbl_expr .form_row').each(function() {
					var index = $(this).data('index'),
						expr_result = response.data.expressions[index],
						result;

					if (response.data.errors[index]) {
						hasErrors = true;
						result = response.data.errors[index];
					}
					else {
						result = expr_result ? <?= json_encode(_('TRUE')) ?> : <?= json_encode(_('FALSE')) ?>;
					}

					switch ($('#expressions_' + index + '_expression_type').val()) {
						case '<?= EXPRESSION_TYPE_INCLUDED ?>':
							expression_type_str = <?= json_encode(_('Character string included')) ?>;
							break;

						case '<?= EXPRESSION_TYPE_ANY_INCLUDED ?>':
							expression_type_str = <?= json_encode(_('Any character string included')) ?>;
							break;

						case '<?= EXPRESSION_TYPE_NOT_INCLUDED ?>':
							expression_type_str = <?= json_encode(_('Character string not included')) ?>;
							break;

						case '<?= EXPRESSION_TYPE_TRUE ?>':
							expression_type_str = <?= json_encode(_('Result is TRUE')) ?>;
							break;

						case '<?= EXPRESSION_TYPE_FALSE ?>':
							expression_type_str = <?= json_encode(_('Result is FALSE')) ?>;
							break;

						default:
							expression_type_str = '';
					}

					$('#testResultTable').append(obj.testTableRowTpl.evaluate({
						expression: $('#expressions_' + index + '_expression').val(),
						type: expression_type_str,
						result: result,
						resultClass: expr_result ? '<?= ZBX_STYLE_GREEN ?>' : '<?= ZBX_STYLE_RED ?>'
					}));
				});

				if (hasErrors) {
					tplData = {
						resultClass: '<?= ZBX_STYLE_RED ?>',
						result: <?= json_encode(_('UNKNOWN')) ?>
					};
				}
				else {
					tplData = {
						resultClass: response.data.final ? '<?= ZBX_STYLE_GREEN ?>' : '<?= ZBX_STYLE_RED ?>',
						result: response.data.final ? <?= json_encode(_('TRUE')) ?> : <?= json_encode(_('FALSE')) ?>
					};
				}

				$('#testResultTable').append(this.testCombinedTableRowTpl.evaluate(tplData));
				$('#testResultTable').css({opacity: 1});
			}
		};
	}(jQuery));

	jQuery(function($) {
		var $form = $('form#regex'),
			$test_string = $('#test_string');
			$test_btn = $('#testExpression');

		$form.on('submit', function() {
			$form.trimValues(['#name']);
		});

		$('#testExpression, #tab_test').click(function() {
			$test_btn.addClass('is-loading');
			$test_btn.prop('disabled', true);
			$test_string.prop('disabled', true);

			zabbixRegExp
				.testExpressions($test_string.val())
				.always(function() {
					$test_btn.removeClass('is-loading');
					$test_btn.prop('disabled', false);
					$test_string.prop('disabled', false);
				});
		});

		$('#tbl_expr').dynamicRows({
			template: '#row_expr'
		});

		$form.find('#clone').click(function() {
			var url = new Curl('zabbix.php?action=regex.edit');

			$form.serializeArray().forEach(function(field) {
				url.setArgument(field.name, field.value);
			});

			redirect(url.getUrl(), 'post', 'action', undefined, false, true);
		});
	});
</script>
